﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web.Mvc;
using Peak.Core;
using Peak.Resources;

namespace Peak.Web.Controllers.Models
{
  public class StoryPreviewModel 
  {
    public TaskFilterModel TaskFilter { get; set; }
    public int Id { get; set; }
    public int SprintStoryId { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
    public int Priority { get; set; }
    public double StoryPoints{ get;set;}
    public StoryStatus Status { get; set; }
    public string Description { get; set; }
    public string HowToDemo { get; set; }
    public string Requester { get; set; }
    public IEnumerable<IGrouping<string, Task>> Tasks { get; set; }
    public TimeSpan RemainingEffort { get; set; }
    public string Created { get; set; }
    public string StartDate { get; set; }
    public string EndDate { get; set; }
    public IList<User> Users { get; set; }

    public StoryPreviewModel(SprintStory sprintStory, TaskFilterModel taskFilter, IList<User> users)
    {
      Name = sprintStory.Story.Name;
      Category = sprintStory.Story.Category;
      Priority = sprintStory.Story.Priority;
      StoryPoints = sprintStory.StoryPoints;
      Status = sprintStory.Status;
      Description = sprintStory.Story.Description;
      HowToDemo = sprintStory.Story.HowToDemo;
      Id = sprintStory.Story.Id;
      SprintStoryId = sprintStory.Id;
      Tasks = sprintStory.Tasks.Where(f=>ContainsStatus(f.Status,taskFilter.Status) && IsAssigned(f.AssignedTo,taskFilter.AssignedTo)).GroupBy(f => f.Category).OrderBy(f=>f.Key);
      TaskFilter = taskFilter;
      RemainingEffort = sprintStory.RemainingEffort;
      Created = sprintStory.Story.CreatedBy + "(" + sprintStory.Story.Created.ToShortDateString() + ")";
      if (sprintStory.Story.StartDate != null)
      {
        StartDate = sprintStory.Story.StartDate.Value.ToShortDateString();
      }
      if (sprintStory.Story.EndDate != null)
      {
        EndDate = sprintStory.Story.EndDate.Value.ToShortDateString();
      }
      Requester = sprintStory.Story.Requester;
      Users = users;
    }

    private bool IsAssigned(User user, string userName)
    {
      if(userName == null)
        return true;

      if(user == null || user.UserName != userName)
        return false;

      return true;
    }

    private bool ContainsStatus(TaskStatus taskStatus, FilterTaskStatus filterTaskStatus)
    {
      if(taskStatus == TaskStatus.ToDo && (filterTaskStatus & FilterTaskStatus.ToDo) == FilterTaskStatus.ToDo)
        return true;
      if (taskStatus == TaskStatus.InProgress && (filterTaskStatus & FilterTaskStatus.InProgress) == FilterTaskStatus.InProgress)
        return true;
      if (taskStatus == TaskStatus.Done && (filterTaskStatus & FilterTaskStatus.Done) == FilterTaskStatus.Done)
        return true;

      if (taskStatus == TaskStatus.ReadyForTest && (filterTaskStatus & FilterTaskStatus.ReadyForTest) == FilterTaskStatus.ReadyForTest)
        return true;

      return false;
    }      
  }
}
